import {Component, Input, OnInit} from '@angular/core';
import {Prodoutbound} from "../../../../model/Prodoutbound";
import {AdminService} from "../../../../services/admin.service";
import {AlertService} from "../../../../services/alert.service";

@Component({
  selector: 'app-cpckedit',
  templateUrl: './cpckedit.component.html',
  styleUrls: ['./cpckedit.component.css']
})
export class CpckeditComponent implements OnInit {
 @Input() id:string
 @Input() optype:string

  p:Prodoutbound
  maxNum:number
  proidArray:Array<any>
  constructor(private adminService: AdminService,
              private alertService: AlertService) {
   this.p = new Prodoutbound()
   this.proidArray = new Array()
   this.maxNum = 1
  }

  ngOnInit(): void {
   this.getproidArray()
   if(this.optype=="update"){
     this.getProdoutboundByid()

   }
  }
  getproidArray(){
    this.adminService.getNzSelCateByTypeid("e4fbf24f284146beb852a297356ebefa").then((data: any) => {
      if (data.msg == "ok") {
        this.proidArray = data.list
      } else {
        this.alertService.showError("获取产品名称失败")
      }
    })
  }

  getProdoutboundByid(){
   this.adminService.getProdoutboundByid(this.id).then((data:any)=>{
     if(data.msg == "ok"){
       this.p = data.obj
       this.test()
     }else{
       this.alertService.showError("根据id获取出库信息失败")
     }
   })
  }
save(){
  this.adminService.saveProdoutbound(this.p).then((data:any)=>{
    if(data.msg == "ok"){
      this.alertService.showSuccess("设置出库信息成功")
    }else{
      this.alertService.showError("设置出库信息失败")
    }
  })
}
  test(){
  this.adminService.getKcslByProid(this.p.proid).then((data:any)=>{
    if(data.msg == "ok"){
      if(this.p.outnum >data.obj){
        this.maxNum=this.p.outnum
      }else{
        this.maxNum = data.obj
      }

    }else if(data.msg == "null"){
      this.alertService.showWarning("没有该商品库存")
    }else{
      this.alertService.showError("获取商品库存失败")
    }
  })

  }
}
